package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ControllerException;
import com.qf.ssm.po.OrderStatus;
import com.qf.ssm.qo.*;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarOrderService;
import com.qf.ssm.utlis.FilesUtils;
import com.qf.ssm.vo.OrderAddVo;
import com.qf.ssm.vo.OrderListVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 黄梓濠
 * @Create 2024-01-02-21:21
 */
@RestController
@RequestMapping("/carorder")
@Api(tags = "汽车租赁管理订单系统")
public class CarOrderController {
    @Autowired
    private CarOrderService carOrderService;
    //查询全部订单状态
    @ApiOperation("查询全部订单的状态列表")
    @PostMapping("/ger/status")
    public ResponseResult<List<OrderStatus>> getOrderStatus() {
        List<OrderStatus> orderStatus = carOrderService.getOrderStatus();
        return ResponseResult.success(orderStatus);
    }

    /**
     *
     * @param page 页数
     * @param size 页量
     * @param orderListQo  订单分页列表qo
     * @return
     */
    //查询订单 (今日取车、今日还车、逾期订单、已取消订单)
    @PostMapping("/get/{page}/{size}")
    @ApiOperation("分页和关键字查询所有订单信息")
    @ApiImplicitParams({@ApiImplicitParam(name ="orderListQo",value="订单条件判断")})
    public ResponseResult<PageInfo<OrderListVo>> getKeywordPageing(@ApiParam(name = "page",value = "页数",required = true)
                                                                       @PathVariable(value = "page" ,required = true) Integer page,
                                                                   @ApiParam(name = "size",value = "页量",required = true)
                                                                   @PathVariable(value = "size" ,required = true) Integer size,
                                                                   @RequestBody(required = false) OrderListQo orderListQo) {
        System.out.println(orderListQo);
        PageInfo<OrderListVo> carOrderVOPageInfo = carOrderService.getKeywordPageing(page, size, orderListQo);
        return ResponseResult.success(carOrderVOPageInfo);
    }


    //获取添加订单原有的数据
    @ApiOperation("获取添加订单原有的数据")
    @PostMapping("/get/order/add")
    public ResponseResult<OrderAddVo> getOrderAdd() {
        OrderAddVo orderAdd = carOrderService.getOrderAdd();
        return ResponseResult.success(orderAdd);
    }

    /**
     *
     * @param orderAddQo 订单添加QO类
     * @return
     */
    //添加订单
    @PostMapping("/add/order")
    @ApiOperation("添加订单")
    public ResponseResult<Integer> addCarOrder(@RequestBody OrderAddQo orderAddQo){
        ResponseResult<Integer> result;
        if (!ObjectUtils.isEmpty(orderAddQo)){
            Integer count = carOrderService.addCarOrder(orderAddQo);
            result = ResponseResult.success(count);
            return result;
        }else {
            System.out.println("数据为空");
            throw new ControllerException(ResultStatus.STATUS_ERROR);
        }
    }

    /**
     *
     * @param orderAddQo 里面的orderId为必要参数
     * @return
     */

    //根据orderid修改订单
    @PostMapping("/edit/carorder")
    @ApiOperation("修改订单信息")
    public ResponseResult<Integer> editCarOrder(@RequestBody OrderAddQo orderAddQo){
        ResponseResult<Integer> result;
        if (!ObjectUtils.isEmpty(orderAddQo)){
            Integer count = carOrderService.editCarOrder(orderAddQo);
            result = ResponseResult.success(count);
            return result;
        }else {
            System.out.println("添加出错");
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
    }

    /**
     *
     * @param orderStatusQO 里面的orderId数组为必要参数
     * @return
     */
    @PostMapping("/edit/status")
    @ApiOperation("设置订单状态")
    //设置订单状态
    public ResponseResult<Integer> editOrderStatus(@RequestBody OrderStatusQO orderStatusQO){
        System.out.println(orderStatusQO);
        if (ObjectUtils.isEmpty(orderStatusQO)){
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
        //传入参数大于5  没有此状态 报错
        if (orderStatusQO.getOrderStatusId() > 5 ||orderStatusQO.getOrderStatusId() < 1){
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
        Integer count = carOrderService.editOrderStatus(orderStatusQO);
        return ResponseResult.success(count);
    }

    /**
     *
     * @param request
     * @param files   文件数组
     * @param orderId 订单编号
     * @return
     */

    //添加合同
    @PostMapping("/add/contract")
    @ApiOperation("添加上传合同")
    public ResponseResult<Integer> addContract(HttpServletRequest request, MultipartFile[] files,Integer orderId){
        String filename = null;
        try {
            filename = FilesUtils.fileUpload(request, files);
        } catch (IOException e) {
           throw new ControllerException(ResultStatus.FILE_UP_ERROR);
        }
        OrderFileQo orderFileQo = new OrderFileQo();
        orderFileQo.setOrderId(orderId);
        orderFileQo.setContract(filename);
        Integer count = carOrderService.addContract(orderFileQo);
        return ResponseResult.success(count);
    }

    /**
     *
     * @param session
     * @param response
     * @param orderId 订单编号
     * @return
     */

    //下载合同
    @PostMapping("/download/contract")
    @ApiOperation("下载回显合同")
    public ResponseResult<String> downloadContract(HttpSession session, HttpServletResponse response,Integer orderId){
        OrderListQo orderListQo = new OrderListQo();
        orderListQo.setOrderId(orderId);
        PageInfo<OrderListVo> keywordPageing = carOrderService.getKeywordPageing(1, 1, orderListQo);
        List<OrderListVo> list = keywordPageing.getList();
        OrderListVo orderListVo = list.get(0);
        String contract = orderListVo.getContract();
        System.out.println(contract);
        try {
            FilesUtils.download(contract,session, response);
        } catch (IOException e) {
            throw new ControllerException(ResultStatus.FILE_DOWNLOAD_FAILURE);
        }
        return ResponseResult.success("下载成功");
    }



}
